package com.pulisher.controller;

import com.pulisher.bean.TradeProvinceCt;
import com.pulisher.bean.TradeScCt;
import com.pulisher.service.TradeStatsService;
import com.pulisher.util.DateFormatUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
//@RequestMapping("/edu/realtime/traffic")
public class TradeController {
    @Autowired
    private TradeStatsService tradeStatsService;

    @RequestMapping("/scCt")
    public String getScCt(@RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<TradeScCt> scCt = tradeStatsService.getScCt(date);
        ArrayList<String> categories = new ArrayList<>();
        ArrayList<BigDecimal> data = new ArrayList<>();
        for (TradeScCt tradeScCt : scCt) {
            categories.add(tradeScCt.getSc_name());
            data.add(tradeScCt.getOrder_amount());
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [\"" + StringUtils.join(categories, "\",\"") + "\"],\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"来源\",\n" +
                "        \"data\": [" + StringUtils.join(data, ",") + "]\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }

    @RequestMapping("/provinceCt")
    public Map getProvinceCt(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TradeProvinceCt> provinceCtList = tradeStatsService.getProvinceCt(date);
        HashMap resMap = new HashMap();
        resMap.put("status", 0);

        HashMap dataMap = new HashMap();
        ArrayList dataList = new ArrayList();
        for (TradeProvinceCt tradeProvinceCt : provinceCtList) {
            HashMap map = new HashMap();
            map.put("name", tradeProvinceCt.getProvinceName());
            map.put("value", tradeProvinceCt.getOrderAmount());
            dataList.add(map);
        }
        dataMap.put("mapData", dataList);
        dataMap.put("valueName", "销售额");
        resMap.put("data", dataMap);
        return resMap;

    }
}
